package org.zeroturnaround.javarebel.integration.ibatis.cbp;

import org.zeroturnaround.bundled.javassist.ClassPool;
import org.zeroturnaround.bundled.javassist.CtClass;
import org.zeroturnaround.bundled.javassist.CtMethod;
import org.zeroturnaround.bundled.javassist.CtNewMethod;
import org.zeroturnaround.javarebel.integration.support.JavassistClassBytecodeProcessor;

/**
 * @author need4spd, need4spd@cplanet.co.kr
 * @date 2009. 11. 25.
 *
 */
public class SqlMapClientTmallCBP extends JavassistClassBytecodeProcessor {
	public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception {
		cp.importPackage("com.ibatis.sqlmap.client");
		cp.importPackage("com.ibatis.common.resources");
		cp.importPackage("java.io");
		cp.importPackage("java.util");
		cp.importPackage("org.zeroturnaround.javarebel.integration.ibatis");

		CtMethod m = ctClass.getMethod("init","(Ljava/net/URL;Ljava/lang/String;)V");
		 m.insertBefore("org.zeroturnaround.javarebel.integration.ibatis.SqlMapXmlFilesManager" +
         ".registerXmlResource($1, $2);");

		 ctClass.addMethod(CtNewMethod.make(
				 "public static void checkAndReload() {" +
				 "	SqlMapXmlFilesManager.checkAndReload(resources,sqlMapFiles); " +
				 "}",
				 ctClass));

		 CtMethod m3 = ctClass.getMethod("getInstance","(Ljava/lang/String;)Lcom/ibatis/sqlmap/client/SqlMapClient;");
		 m3.insertBefore("checkAndReload();");
	}
}
